from goods.models import GoodsChannel


def get_categories():
    categories = {}
    channels = GoodsChannel.objects.order_by('group_id', 'sequence')
    for channel in channels:
        group_id = channel.group_id

        if group_id not in categories:
            categories[group_id] = {'channels':[], 'sub_cats': []}

        category1 = channel.category
        category1_dict = {
            'id':channel.id,
            'name':category1.name,
            'url':channel.url
        }

        categories[group_id]['channels'].append(category1_dict)

        cats2 = category1.subs.all()
        for cat2 in cats2:
            categories[group_id]['sub_cats'].append(cat2)

    return categories